package cn.guetzjb.sys.controller;

import cn.guetzjb.core.entity.R;
import cn.guetzjb.core.utils.UploadUtils;
import cn.guetzjb.security.utils.SecurityUtils;
import cn.guetzjb.sys.domain.SysFile;
import cn.guetzjb.sys.service.SysFileService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@RestController
@RequestMapping("/file/manage")
@RequiredArgsConstructor
@Tag(name = "文件管理", description = "文件管理")
public class FileManageController {

    private final SysFileService service;

    @GetMapping("/all")
    @Operation(summary = "列表(page)")
    public R pageAll(@RequestParam(required = false, defaultValue = "1") Integer current,
                     @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                     @RequestParam(required = false, defaultValue = "") String fileName,
                     @RequestParam(required = false, defaultValue = "") String fileType,
                     @RequestParam(required = false, defaultValue = "-1") Double fileSize) {
        Page<SysFile> page = service.page(new Page<>(current, pageSize),
                Wrappers.<SysFile>lambdaQuery()
                        .like(StringUtils.isNotEmpty(fileName), SysFile::getFileName, fileName)
                        .like(StringUtils.isNotEmpty(fileType), SysFile::getFileType, fileType)
                        .le(fileSize != -1d, SysFile::getFileSize, fileSize)
                        .orderByDesc(SysFile::getCreateTime));
        return R.ok(page);
    }

    @PostMapping("/update")
    @Operation(summary = "更新文件")
    public R updateFile(@RequestBody SysFile sysFile) {
        return R.ok(service.updateById(sysFile));
    }

    @PostMapping("/delete/{ids}")
    @Operation(summary = "删除文件")
    public R deleteFiles(@PathVariable List<Integer> ids) {
        return R.ok(service.removeByIds(ids));
    }

    @PostMapping("/add")
    @Operation(summary = "新增文件")
    public R addFile(MultipartFile file) {
        if(file == null){
            return R.failed("请选择文件上传");
        }
        String fileUrl = UploadUtils.uploadToServer(file, false);
        if("".equals(fileUrl)){
            return R.failed("上传失败");
        }
        SysFile sysFile = new SysFile();
        sysFile.setCreateBy(SecurityUtils.getUsername());
        sysFile.setFileUrl(fileUrl);
        sysFile.setFileName(file.getOriginalFilename());
        sysFile.setFileSize(BigDecimal.valueOf((double) file.getSize() / (1024 * 1024)).setScale(2, RoundingMode.HALF_UP).doubleValue());
        sysFile.setFileType(file.getContentType());
        return R.ok(service.save(sysFile));
    }

}
